/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.controller.adminServlet;

import presentation.controller.adminServlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.DAO.UserDAOServiceImpl;
import model.entity.User;

/**
 *
 * @author ninkuhack: Quản lý user của admin, liệt kê các user của người dùng,
 * cho phép sửa, xóa thông tin user bất kỳ Phước thực hiện
 */
@WebServlet(name = "UserManagementServlet", urlPatterns = {"/UserManagementServlet"})
public class UserManagementServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UserManagementServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UserManagementServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("submit").equals("ViewUser")) {
            showListUser(request, response);
        }
        if (request.getParameter("submit").equals("UpdateUser")) {
            updateUser(request, response);
        }
        if (request.getParameter("submit").equals("DeleteUser")) {
            deletedUser(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("submit").equals("ViewUser")) {
            showListUser(request, response);
        }
        if (request.getParameter("submit").equals("UpdateUser")) {
            updateUser(request, response);
        }
        if (request.getParameter("submit").equals("DeleteUser")) {
            deletedUser(request, response);
        }
    }

    protected void showListUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        UserDAOServiceImpl userdao = new UserDAOServiceImpl();
        userdao.getAllUser();
        response.sendRedirect("abc.jsp");

    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        User user = new User();
        int userID = Integer.parseInt(request.getParameter("user_id"));
        user.setUserID(userID);
        user.setFullName(request.getParameter("fullName"));
        user.setAddress(request.getParameter("address"));
        user.setCity(request.getParameter("city"));
        user.setEmail(request.getParameter("email"));
        user.setPhone(request.getParameter("phone"));
        user.setPoint(Integer.parseInt(request.getParameter("point")));
        user.setStatus(Integer.parseInt(request.getParameter("status")));

        UserDAOServiceImpl userdao = new UserDAOServiceImpl();
        userdao.updateUserByAdmin(user);
    }

    private void deletedUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        User user = new User();
        int userID = Integer.parseInt(request.getParameter("user_id"));
        UserDAOServiceImpl userdao = new UserDAOServiceImpl();
        userdao.deleteUser(userID);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
